package com.fit.pages;

import java.util.LinkedList;

import org.apache.tapestry5.annotations.Component;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.SetupRender;
import org.apache.tapestry5.corelib.components.Form;
import org.apache.tapestry5.corelib.components.PasswordField;
import org.apache.tapestry5.corelib.components.TextField;
import org.apache.tapestry5.ioc.Messages;
import org.apache.tapestry5.ioc.annotations.Inject;

import com.fit.beans.UserBean;
import com.fit.enums.AccountRole;
import com.fit.enums.Gender;
import com.fit.interfaces.BorrowI;
import com.fit.interfaces.User;
import com.fit.service.Users;
import com.fit.services.AppModule;

public class Registration {

	@Persist
	@Property
	private User user;
	
	@Inject
	private Users users;
	
	@SetupRender
	public void createObject(){
		user = (User) AppModule.context.getBean("user");
		user.setBorrows(new LinkedList<BorrowI>());
		
	}
	
	@Component(id="registrationForm")
	private Form registrationForm;
	
	@Component(id="userName")
	private TextField userNameField;
	
	@Component(id = "password")
	private PasswordField passwordField;
	

	
	@Persist
	private String userName;
	
	@Persist
	private String password;
	
	@Persist
	private String password2;
	
	@Persist
	@Property
	private String fullName;
	
	@Persist
	@Property
	private Gender gender;
	
	@Persist
	@Property
	private String email;
	
	
	
	@Inject
	private Messages messages;
	
	public void onValidateForm(){

		if(!((userName == null) || (password == null) || (password2 ==null) ) ){
				if(!password.equals(password2)){
					registrationForm.recordError(passwordField, messages.get("passwords don't match"));
				}
				if(users.retriveSingleUser(getUserName()) != null){
					registrationForm.recordError(userNameField, messages.get("userName-exists"));
				}

		}
	}
	
	public boolean isPasswordNotSubmitted() {
		System.out.println("Pass1: " + password);
		System.out.println("Pass2: " + password2);
		System.out.println("Provjera1: " + ((password == null)));
		if (password != null) System.out.println("Provjera2: " + ((!password.equals(password2))));
		return (password == null) || (!password.equals(password2));
	}
	
	 Object onSuccess()
	    {
//		 user.executeUserCreation(userName, password, fullName, gender, email, role.USER);
		 user.executeUserForRegistration(userName, password, fullName, gender, email);
		 users.save(user);
		 userName="";
		 password="";
		 fullName="";
		 gender=null;
		 email="";
//		 role=null;
		 return null;
	    }
	 
	//Setters and getters
	 
	public Gender getMale(){
		return user.getGender().MALE;
	}
	
	public Gender getFemale(){
		return user.getGender().FEMALE;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getPassword2() {
		return password2;
	}

	public void setPassword2(String password2) {
		this.password2 = password2;
	}
	
}
